
### This code produces the tables and assumes that the models in models.do have been run in Stata 

rm(list=ls(all=TRUE))

set.seed(123)

library(foreign)
library(MCMCpack)
library(xtable)

setwd("replication")

data <- read.dta("data.dta")



## TABLE 1
load("coefs.rda")


m01 <- as.mcmc(coefs$coef_01)
msum <- summary(m01)

tab <- NULL
for(i in 1:8){
	add <- matrix(c(rownames(msum$statistics)[i], format(round(msum$statistics[i,1], 5), nsmall=5, scientific=10), "", paste0("(", format(round(msum$quantiles[i,1], 5), nsmall=5, scientific=10), ", ", format(round(msum$quantiles[i,5], 5), nsmall=5, scientific=10), ")")), nrow=2, byrow=T)
	tab <- rbind(tab, add)
}

m01tab <- tab


m05 <- as.mcmc(coefs$coef_05)
msum <- summary(m05)

tab <- NULL
for(i in 1:8){
	add <- matrix(c(rownames(msum$statistics)[i], format(round(msum$statistics[i,1], 5), nsmall=5, scientific=10), "", paste0("(", format(round(msum$quantiles[i,1], 5), nsmall=5, scientific=10), ", ", format(round(msum$quantiles[i,5], 5), nsmall=5, scientific=10), ")")), nrow=2, byrow=T)
	tab <- rbind(tab, add)
}

m05tab <- tab


m25 <- as.mcmc(coefs$coef_25)
msum <- summary(m25)

tab <- NULL
for(i in 1:8){
	add <- matrix(c(rownames(msum$statistics)[i], format(round(msum$statistics[i,1], 5), nsmall=5, scientific=10), "", paste0("(", format(round(msum$quantiles[i,1], 5), nsmall=5, scientific=10), ", ", format(round(msum$quantiles[i,5], 5), nsmall=5, scientific=10), ")")), nrow=2, byrow=T)
	tab <- rbind(tab, add)
}

m25tab <- tab


m50 <- as.mcmc(coefs$coef_50)
msum <- summary(m50)

tab <- NULL
for(i in 1:8){
	add <- matrix(c(rownames(msum$statistics)[i], format(round(msum$statistics[i,1], 5), nsmall=5, scientific=10), "", paste0("(", format(round(msum$quantiles[i,1], 5), nsmall=5, scientific=10), ", ", format(round(msum$quantiles[i,5], 5), nsmall=5, scientific=10), ")")), nrow=2, byrow=T)
	tab <- rbind(tab, add)
}

m50tab <- tab


m75 <- as.mcmc(coefs$coef_75)
msum <- summary(m75)

tab <- NULL
for(i in 1:8){
	add <- matrix(c(rownames(msum$statistics)[i], format(round(msum$statistics[i,1], 5), nsmall=5, scientific=10), "", paste0("(", format(round(msum$quantiles[i,1], 5), nsmall=5, scientific=10), ", ", format(round(msum$quantiles[i,5], 5), nsmall=5, scientific=10), ")")), nrow=2, byrow=T)
	tab <- rbind(tab, add)
}

m75tab <- tab


m95 <- as.mcmc(coefs$coef_95)
msum <- summary(m95)

tab <- NULL
for(i in 1:8){
	add <- matrix(c(rownames(msum$statistics)[i], format(round(msum$statistics[i,1], 5), nsmall=5, scientific=10), "", paste0("(", format(round(msum$quantiles[i,1], 5), nsmall=5, scientific=10), ", ", format(round(msum$quantiles[i,5], 5), nsmall=5, scientific=10), ")")), nrow=2, byrow=T)
	tab <- rbind(tab, add)
}

m95tab <- tab


m99 <- as.mcmc(coefs$coef_99)
msum <- summary(m99)

tab <- NULL
for(i in 1:8){
	add <- matrix(c(rownames(msum$statistics)[i], format(round(msum$statistics[i,1], 5), nsmall=5, scientific=10), "", paste0("(", format(round(msum$quantiles[i,1], 5), nsmall=5, scientific=10), ", ", format(round(msum$quantiles[i,5], 5), nsmall=5, scientific=10), ")")), nrow=2, byrow=T)
	tab <- rbind(tab, add)
}

m99tab <- tab


vars <- c("Growth GDPpc", "", "Trade Openness", "", "Population (log)", "", "Election Year", "", "Number of Parties", "", "Number of Events", "", "Mean Cooperation Score", "", "Constant", "")
outtable <- xtable(cbind(vars, m01tab[,2], m05tab[,2], m25tab[,2], m50tab[,2], m75tab[,2], m95tab[,2], m99tab[,2]))
print(outtable)




## TABLE 2, PANEL 1
load("coefs_fe.rda")


m01 <- as.mcmc(coefs$coef_01)
msum <- summary(m01)

tab <- NULL
for(i in 1:18){
	add <- matrix(c(rownames(msum$statistics)[i], format(round(msum$statistics[i,1], 5), nsmall=5, scientific=10), "", paste0("(", format(round(msum$quantiles[i,1], 5), nsmall=5, scientific=10), ", ", format(round(msum$quantiles[i,5], 5), nsmall=5, scientific=10), ")")), nrow=2, byrow=T)
	tab <- rbind(tab, add)
}

m01tab <- tab


m05 <- as.mcmc(coefs$coef_05)
msum <- summary(m05)

tab <- NULL
for(i in 1:18){
	add <- matrix(c(rownames(msum$statistics)[i], format(round(msum$statistics[i,1], 5), nsmall=5, scientific=10), "", paste0("(", format(round(msum$quantiles[i,1], 5), nsmall=5, scientific=10), ", ", format(round(msum$quantiles[i,5], 5), nsmall=5, scientific=10), ")")), nrow=2, byrow=T)
	tab <- rbind(tab, add)
}

m05tab <- tab



m25 <- as.mcmc(coefs$coef_25)
msum <- summary(m25)

tab <- NULL
for(i in 1:18){
	add <- matrix(c(rownames(msum$statistics)[i], format(round(msum$statistics[i,1], 5), nsmall=5, scientific=10), "", paste0("(", format(round(msum$quantiles[i,1], 5), nsmall=5, scientific=10), ", ", format(round(msum$quantiles[i,5], 5), nsmall=5, scientific=10), ")")), nrow=2, byrow=T)
	tab <- rbind(tab, add)
}

m25tab <- tab


m50 <- as.mcmc(coefs$coef_50)
msum <- summary(m50)

tab <- NULL
for(i in 1:18){
	add <- matrix(c(rownames(msum$statistics)[i], format(round(msum$statistics[i,1], 5), nsmall=5, scientific=10), "", paste0("(", format(round(msum$quantiles[i,1], 5), nsmall=5, scientific=10), ", ", format(round(msum$quantiles[i,5], 5), nsmall=5, scientific=10), ")")), nrow=2, byrow=T)
	tab <- rbind(tab, add)
}

m50tab <- tab


m75 <- as.mcmc(coefs$coef_75)
msum <- summary(m75)

tab <- NULL
for(i in 1:18){
	add <- matrix(c(rownames(msum$statistics)[i], format(round(msum$statistics[i,1], 5), nsmall=5, scientific=10), "", paste0("(", format(round(msum$quantiles[i,1], 5), nsmall=5, scientific=10), ", ", format(round(msum$quantiles[i,5], 5), nsmall=5, scientific=10), ")")), nrow=2, byrow=T)
	tab <- rbind(tab, add)
}

m75tab <- tab


m95 <- as.mcmc(coefs$coef_95)
msum <- summary(m95)

tab <- NULL
for(i in 1:18){
	add <- matrix(c(rownames(msum$statistics)[i], format(round(msum$statistics[i,1], 5), nsmall=5, scientific=10), "", paste0("(", format(round(msum$quantiles[i,1], 5), nsmall=5, scientific=10), ", ", format(round(msum$quantiles[i,5], 5), nsmall=5, scientific=10), ")")), nrow=2, byrow=T)
	tab <- rbind(tab, add)
}

m95tab <- tab


m99 <- as.mcmc(coefs$coef_99)
msum <- summary(m99)

tab <- NULL
for(i in 1:18){
	add <- matrix(c(rownames(msum$statistics)[i], format(round(msum$statistics[i,1], 5), nsmall=5, scientific=10), "", paste0("(", format(round(msum$quantiles[i,1], 5), nsmall=5, scientific=10), ", ", format(round(msum$quantiles[i,5], 5), nsmall=5, scientific=10), ")")), nrow=2, byrow=T)
	tab <- rbind(tab, add)
}

m99tab <- tab


vars <- c("Growth GDPpc", "", "Trade Openness", "", "Population (log)", "", "Election Year", "", "Number of Parties", "", "Number of Events", "", "Mean Cooperation Score", "", "Constant", "")
outtable <- xtable(cbind(vars, m01tab[c(1:14, 35, 36),2], m05tab[c(1:14, 35, 36),2], m25tab[c(1:14, 35, 36),2], m50tab[c(1:14, 35, 36),2], m75tab[c(1:14, 35, 36),2], m95tab[c(1:14, 35, 36),2], m99tab[c(1:14, 35, 36),2]))
print(outtable)






## TABLE 2, PANEL 2
load("coefs_2fe.rda")


m01 <- as.mcmc(coefs$coef_01)
msum <- summary(m01)

tab <- NULL
for(i in 1:28){
	add <- matrix(c(rownames(msum$statistics)[i], format(round(msum$statistics[i,1], 5), nsmall=5, scientific=10), "", paste0("(", format(round(msum$quantiles[i,1], 5), nsmall=5, scientific=10), ", ", format(round(msum$quantiles[i,5], 5), nsmall=5, scientific=10), ")")), nrow=2, byrow=T)
	tab <- rbind(tab, add)
}

m01tab <- tab


m05 <- as.mcmc(coefs$coef_05)
msum <- summary(m05)

tab <- NULL
for(i in 1:28){
	add <- matrix(c(rownames(msum$statistics)[i], format(round(msum$statistics[i,1], 5), nsmall=5, scientific=10), "", paste0("(", format(round(msum$quantiles[i,1], 5), nsmall=5, scientific=10), ", ", format(round(msum$quantiles[i,5], 5), nsmall=5, scientific=10), ")")), nrow=2, byrow=T)
	tab <- rbind(tab, add)
}

m05tab <- tab



m25 <- as.mcmc(coefs$coef_25)
msum <- summary(m25)

tab <- NULL
for(i in 1:28){
	add <- matrix(c(rownames(msum$statistics)[i], format(round(msum$statistics[i,1], 5), nsmall=5, scientific=10), "", paste0("(", format(round(msum$quantiles[i,1], 5), nsmall=5, scientific=10), ", ", format(round(msum$quantiles[i,5], 5), nsmall=5, scientific=10), ")")), nrow=2, byrow=T)
	tab <- rbind(tab, add)
}

m25tab <- tab


m50 <- as.mcmc(coefs$coef_50)
msum <- summary(m50)

tab <- NULL
for(i in 1:28){
	add <- matrix(c(rownames(msum$statistics)[i], format(round(msum$statistics[i,1], 5), nsmall=5, scientific=10), "", paste0("(", format(round(msum$quantiles[i,1], 5), nsmall=5, scientific=10), ", ", format(round(msum$quantiles[i,5], 5), nsmall=5, scientific=10), ")")), nrow=2, byrow=T)
	tab <- rbind(tab, add)
}

m50tab <- tab


m75 <- as.mcmc(coefs$coef_75)
msum <- summary(m75)

tab <- NULL
for(i in 1:28){
	add <- matrix(c(rownames(msum$statistics)[i], format(round(msum$statistics[i,1], 5), nsmall=5, scientific=10), "", paste0("(", format(round(msum$quantiles[i,1], 5), nsmall=5, scientific=10), ", ", format(round(msum$quantiles[i,5], 5), nsmall=5, scientific=10), ")")), nrow=2, byrow=T)
	tab <- rbind(tab, add)
}

m75tab <- tab


m95 <- as.mcmc(coefs$coef_95)
msum <- summary(m95)

tab <- NULL
for(i in 1:28){
	add <- matrix(c(rownames(msum$statistics)[i], format(round(msum$statistics[i,1], 5), nsmall=5, scientific=10), "", paste0("(", format(round(msum$quantiles[i,1], 5), nsmall=5, scientific=10), ", ", format(round(msum$quantiles[i,5], 5), nsmall=5, scientific=10), ")")), nrow=2, byrow=T)
	tab <- rbind(tab, add)
}

m95tab <- tab


m99 <- as.mcmc(coefs$coef_99)
msum <- summary(m99)

tab <- NULL
for(i in 1:28){
	add <- matrix(c(rownames(msum$statistics)[i], format(round(msum$statistics[i,1], 5), nsmall=5, scientific=10), "", paste0("(", format(round(msum$quantiles[i,1], 5), nsmall=5, scientific=10), ", ", format(round(msum$quantiles[i,5], 5), nsmall=5, scientific=10), ")")), nrow=2, byrow=T)
	tab <- rbind(tab, add)
}

m99tab <- tab


vars <- c("Growth GDPpc", "", "Trade Openness", "", "Population (log)", "", "Election Year", "", "Number of Parties", "", "Number of Events", "", "Mean Cooperation Score", "", "Constant", "")
outtable <- xtable(cbind(vars, m01tab[c(1:14, 55, 56),2], m05tab[c(1:14, 55, 56),2], m25tab[c(1:14, 55, 56),2], m50tab[c(1:14, 55, 56),2], m75tab[c(1:14, 35, 36),2], m95tab[c(1:14, 55, 56),2], m99tab[c(1:14, 55, 56),2]))
print(outtable)


